﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Application.Task;
using MvcPresentation.Models;

namespace MvcPresentation.Controllers
{
    public class CustomersController : Controller
    {
        //
        // GET: /Customers/

        public ActionResult Index()
        {
            var task = new RetrieveAllCustomers();
            task.Execute();
            var model = new Models.CustomerMapper().From(task.Result.Entities);
            return View(model);
        }

        public ActionResult Create()
        {
            var model = new Customer();
            return View(model);
        }

        [HttpPost]
        public ActionResult Create(Customer customer)
        {
            if (ModelState.IsValid)
            {
                var dto = new Models.CustomerMapper().From(customer);
                var task = new CreateNewCustomer(dto);
                task.Execute();
                if (task.Succes)
                {
                   return RedirectToAction("Index");

                }
                else
                {
                    foreach (var item in task.BrokenRules)
                    {
                        ModelState.AddModelError(string.Empty, item.ErrorMessage);
                    }

                }
            }

            return View(customer);


        }


        public ActionResult Edit(int id)
        {
            var task = new RetrieveCustomerById(id);
            task.Execute();
            if (task.CustomerInfo!=null)
            {
                var model = new Models.CustomerMapper().From(task.CustomerInfo);
                return View(model);
            }
            else
            {
                return View("NotFound");
            }
        }

        [HttpPost]
        public ActionResult Edit(int id, FormCollection formValues)
        {

            var read = new RetrieveCustomerById(id);
            read.Execute();
            if (read.CustomerInfo != null)
            {
                var model = new Models.CustomerMapper().From(read.CustomerInfo);
                if (TryUpdateModel(model))
                {
                    var dto = new Models.CustomerMapper().From(model);
                    var update = new ChangeCustomerDetails(dto);
                    update.Execute();
                    if (update.Succes)
                    {
                        return RedirectToAction("Details", new {id = model.CustomerId});
                    }
                    else
                    {
                        foreach (var item in update.BrokenRules)
                        {
                            ModelState.AddModelError(string.Empty, item.ErrorMessage);
                        }
                    }
                 
                }
                return View(model);
            }
            else
            {
                return View("NotFound");
            }
        }

        public ActionResult Details(int id)
        {
            var task = new RetrieveCustomerById(id);
            task.Execute();
            if (task.CustomerInfo != null)
            {
                var model = new Models.CustomerMapper().From(task.CustomerInfo);
                return View(model);
            }
            else
            {
                return View("NotFound");
            }
        }

        public ActionResult Delete(int id)
        {
            var task = new RetrieveCustomerById(id);
            task.Execute();
            if (task.CustomerInfo != null)
            {
                var model = new Models.CustomerMapper().From(task.CustomerInfo);
                return View(model);
            }
            else
            {
                return View("NotFound");
            }
        }

        [HttpPost]
        public ActionResult Delete(int id, string confirmButton)
        {
            var task = new RemoveCustomer(id);
            task.Execute();
            return RedirectToAction("Index"); 
        }

    }
}
